/* 
 * File:   main.cpp
 */
#include <stdlib.h>
#include <cstdlib>
#include <fstream>
#include <vmime/vmime.hpp>
#include <vmime/platforms/posix/posixHandler.hpp>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>



#include "RemoteAdress.h"
#include "MyDate.h"
#include "RemoteSource.h"
#include "Manager.h"
#include "Help.h"
#include "Log.h"
#include "LocalSource.h"

using namespace std;



void conf (Account& me_);

void templ (string& file);

int
main (int argc, char** argv)
{


  vmime::platform::setHandler<vmime::platforms::posix::posixHandler > ();
  Account me;
  conf (me); // configing log there too

  char r[] = "r";
  char s[] = "s";
  char rd[] = "rd";
  char ld[] = "ld";
  char lv[] = "lv";
  char rv[] = "rv";
  char rc[] = "rc";
  char rr[] = "-r";
  char ll[] = "-l";
  char h[] = "-h";
  char v[] = "-v";
  char mama[] = "files";



    //argv[1] = r;
     // argv[2] = rr;
     //argv[3] = mama;




  int i = 1;

  if ((argv[i] != NULL) && ((strcmp (argv[i], "-a") == 0) || (strcmp (argv[i], "--archive") == 0)))
    {
      me.if_archive_flag = true;
      ++i;
    }

  if ((argv[i] != NULL) && ((strcmp (argv[i], "-h") == 0) || ((strcmp (argv[i], "--help") == 0))))
    {

      cout << help;
      return 0;
    }


  if ((argv[i] != NULL) && ((strcmp (argv[i], "-v") == 0) || ((strcmp (argv[i], "--verbose") == 0))))
    {
      Log::getInstance ()->verb = true;
      ++i;
    }

  

  char* com;
  com = argv[i];
  ++i;


  if ((argv[i] != NULL) && (strcmp (argv[i], "-s") == 0))
    {
      string s (argv[++i]);
      me.synch = s;
      ++i;
    };

  if ((argv[i] != NULL) && (strcmp (argv[i], "-r") == 0))
    {
      string s (argv[++i]);
      me.receive_adress.adv_adr = s;
      me.receive_adress.if_adv_adr = true;
      ++i;
    };

  if ((argv[i] != NULL) && (strcmp (argv[i], "-l") == 0))
    {
      string s (argv[++i]);
      me.local_adress.adv_adr = s;
      me.local_adress.adv_adr_ = true;
      ++i;

    };

  if ((argv[i] != NULL) && (strcmp (argv[i], "-r") == 0))
    {
      string s (argv[++i]);
      me.receive_adress.adv_adr = s;
      me.receive_adress.if_adv_adr = true;
      ++i;
    };




  if ((com != NULL) && ((strcmp (com, "rv") == 0) || (strcmp (com, "RV") == 0) || (strcmp (com, "REMOTE_VIEW") == 0)))
    {
      Manager man (me);
      man.doRView ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    };

  if ((com != NULL) && ((strcmp (com, "lv") == 0) || (strcmp (com, "LV") == 0) || (strcmp (com, "LOCAL_VIEW") == 0)))
    {
      Manager man (me);
      man.doLView ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    };

  if ((com != NULL) && ((strcmp (com, "s") == 0) || (strcmp (com, "S") == 0) || (strcmp (com, "SEND") == 0) || (strcmp (com, "send") == 0)))
    {
      Manager man (me);
      man.doRecSenSyn (2);
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }


  if ((com != NULL) && ((strcmp (com, "r") == 0) || (strcmp (com, "R") == 0) || (strcmp (com, "RECEIVE") == 0) || (strcmp (com, "receive") == 0)))
    {
      Manager man (me);
      man.doRecSenSyn (1);
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }

  if ((com != NULL) && ((strcmp (com, "rd") == 0) || (strcmp (com, "RD") == 0) || (strcmp (com, "REMOTE_DELETE") == 0)))
    {
      Manager man (me);
      man.doRDelete ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }

  if ((com != NULL) && ((strcmp (com, "ld") == 0) || (strcmp (com, "LD") == 0) || (strcmp (com, "LOCAL_DELETE") == 0)))
    {
      Manager man (me);
      man.doLDelete ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }

  if ((com != NULL) && ((strcmp (com, "rc") == 0) || (strcmp (com, "RC") == 0) || (strcmp (com, "REMOTE_CLEAR") == 0)))
    {
      Manager man (me);
      man.doRClear ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }

  if ((com != NULL) && ((strcmp (com, "lc") == 0) || (strcmp (com, "LC") == 0) || (strcmp (com, "LOCAL_CLEAR") == 0)))
    {
      Manager man (me);
      man.doLClear ();
      Log::getInstance ()->sDebugging ("Succesfull run of the programm");
      return 0;
    }



  Log::getInstance ()->sMessage ("USAGE: emailsynch [OPTIONS]... COMMAND [-r REMOTESOURCE] [-l LOCALSOURCE] ");
  Log::getInstance ()->sMessage (" Try 'emailsynch --help' for more information ");

  return 0;
}

void
conf (Account& me_)
{

  me_.local_adress.adv_adr_ = false;

  me_.receive_adress.auth = true;
  me_.receive_adress.protocol_name = "imap";
  me_.receive_adress.ssl = false;
  me_.receive_adress.tls = false;

  me_.sending_adress.auth = true;
  me_.sending_adress.protocol_name = "smtp";
  me_.sending_adress.ssl = false;
  me_.sending_adress.tls = false;

  me_.if_archive_flag = false;
  me_.if_synch = false;
  me_.receive_adress.if_adv_adr = false;
  me_.local_adress.adv_adr_ = false;


  struct passwd *pw = getpwuid (getuid ());
  const char *homedir = pw->pw_dir;

  string home (homedir);
  string conffile = home + "/.emailsynch-conf";
  string logfile = home + "/.emailsynch-log";

  char curdir[1000];
  getcwd (curdir, sizeof (curdir));

 


  struct stat* attr;
  attr = (struct stat*) malloc (sizeof (struct stat));
  const char* cbuf = (logfile).c_str ();
  stat (cbuf, attr);
  if (attr->st_size > 10000000)
    remove (logfile.c_str ());


  Log::getInstance ()->out.open (logfile.c_str (), ios::app);
  Log::getInstance ()->filewrite = true;
  Log::getInstance ()->sDebugging ("\n\n\n\n");



  if (!LocalSource::FileExists (conffile))
    {
      templ (conffile);
      Log::getInstance ()->sMessage ("Config file was just made in " + conffile + ".\n To use programm set your parameters.");
      return;
    }



  ifstream sett (conffile.c_str ());
  if (!sett)
    {
      Log::getInstance ()->sMessage ("Cant open config file");
    }

  string s;
  string par;

  while (sett)
    {
      sett >> s;

      bool flag = true;

      for (int i = 0; i != s.size (); ++i)
        if ((s[i] == ':') && (s[i + 1] == ':'))
          {
            flag = false;
            int n = i;
            par = s;
            par.erase (n, s.size () - n);
            s.erase (0, n + 2);
            break;
          };

          if ((s.size () == 0) || (flag))
            continue;

      if (par == "rserver")
        {
          me_.receive_adress.server_url = s;
        }

      if (par == "rport")
        {
          me_.receive_adress.port = atoi (s.c_str ());
        }

      if (par == "sserver")
        {
          me_.sending_adress.server_url = s;
        }

      if (par == "sport")
        {
          me_.sending_adress.port = atoi (s.c_str ());
        }
      if (par == "username")
        {
          me_.receive_adress.username = s;
          me_.sending_adress.username = s;
        }
      if (par == "passwd")
        {
          me_.receive_adress.passwd = s;
          me_.sending_adress.passwd = s;
        }


      if (par == "rauth")
        {
          if (s == "true")
            me_.receive_adress.auth = true;
          if (s == "false")
            me_.receive_adress.auth = false;
        }
      if (par == "rmailbox")
        {
          me_.receive_adress.mailbox = s;
        }
      if (par == "rpasswd")
        {
          me_.receive_adress.passwd = s;
        }
      if (par == "rport")
        {

          me_.receive_adress.port = atoi (s.c_str ());
        }
      if (par == "rprotocol")
        {
          me_.receive_adress.protocol_name = s;
        }
      if (par == "rserver")
        {
          me_.receive_adress.server_url = s;
        }
      if (par == "rssl")
        {
          if (s == "true")
            me_.receive_adress.ssl = true;
          if (s == "false")
            me_.receive_adress.ssl = false;
        }
      if (par == "rusername")
        {
          me_.receive_adress.username = s;
        }
      if (par == "sauth")
        {
          if (s == "true")
            me_.sending_adress.auth = true;
          if (s == "false")
            me_.sending_adress.auth = false;
        }
      if (par == "smailbox")
        {
          me_.sending_adress.mailbox = s;
        }
      if (par == "spasswd")
        {
          me_.sending_adress.passwd = s;
        }
      if (par == "sport")
        {
          me_.sending_adress.port = atoi (s.c_str ());
        }
      if (par == "sprotocol")
        {
          me_.sending_adress.protocol_name = s;
        }
      if (par == "sserver")
        {
          me_.sending_adress.server_url = s;
        }
      if (par == "sssl")
        {
          if (s == "true")
            me_.sending_adress.ssl = true;
          if (s == "false")
            me_.sending_adress.ssl = false;
        }
      if (par == "stsl")
        {
          if (s == "true")
            me_.sending_adress.tls = true;
          if (s == "false")
            me_.sending_adress.tls = false;
        }
      if (par == "susername")
        {
          me_.sending_adress.username = s;
        }

    };

    //!!!Set current dir as default dir


};

void
templ (string& file)
{
  ofstream f;
  f.open (file.c_str (), ios::out);
  f << "This is config file for email-synch tool. To use the programm you should fill first block. If it wouldn't work, fill another one." << endl;
  f << "prefix 'p' means that parameters for receiving. prefix 's' means that parameters for sending." << endl;

  f << "rserver::" << endl;
  f << "// usaully 'imap.some_domain.com' " << endl;
  f << "rport::" << endl;
  f << "sserver::" << endl;
  f << "// usaully 'smtp.some_domain.com' " << endl;
  f << "sport::" << endl;
  f << "username::" << endl;
  f << "// usually username is mailbox" << endl;
  f << "passwd::" << endl;
  f << endl << endl;

  

  f << "rauth::" << endl;
  f << "rmailbox::" << endl;
  f << "rpasswd::" << endl;
  f << "rport::" << endl;
  f << "rprotocol::" << endl;
  f << "// try 'imaps' if imap doesn't work" << endl;
  f << "rserver::" << endl;
  f << "rssl::" << endl;
  f << "rusername::" << endl;

  f << "sauth::" << endl;
  f << "smailbox::" << endl;
  f << "spasswd::" << endl;
  f << "sport::" << endl;
  f << "sprotocol::" << endl;
  f << "sserver::" << endl;
  f << "sssl::" << endl;
  f << "stsl::" << endl;
  f << "susername::" << endl;

};
